<template>
  <el-dialog :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名" style="width: 195px;"></el-input>
        <el-button size="small" class="in_block" style="margin-left: 20px;" @click="readIdCard(peopleIndex)">读取身份证</el-button>
      </el-form-item>
      <el-form-item label="证件类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择" size="small">
          <el-option v-for="item in documenttypelist" :key="item.id" :label="item.label" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择" size="small">
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="证件号" prop="certificatesNumber">
        <el-input v-model="dataForm.certificatesNumber" placeholder="证件号"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        options: {
          housOptions: [],
          paysTypeOptions: [],
          documenttypelist: [],
        },
        istrues: 0,
        dataForm: {
          id: 0,
          sellerId: '',
          creatTime: '',
          type: '',
          sex: 1,
          address: '',
          name: '',
          tel: '',
          certificatesNumber: '',
          floorRoomId: '',
          arrivalsTime: '',
          predissociationTime: '',
          payment: '',
          roomPrice: '',
          consumption: ''
        },
        dataRule: {
          creatTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          type: [{
            required: true,
            message: '身份类型必选',
            trigger: 'blur'
          }],
          name: [{
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      zjlx() {
        this.$comReq.reqDictionaryData("document_type")
          .then(({
            data
          }) => {
            this.documenttypelist = data.page.list
          })
      },
      readIdCard(index) {
        var CardEntity = {
          Name: "张三",
          Sex: "男"
        };
        var postData = {
          DeviceType: "IDCardHareware",
          Params: JSON.stringify(CardEntity)
        };
        this.Post(postData, "Read", index);
      },
      Post(postData, methods, index) {
        console.log(postData)
        this.istrues = 0;
        var result;
        this.axios({
          url: this.axios.postUrl(`/api/Service/` + methods),
          method: 'POST',
          data: postData,
          cache: false,
          async: false,
          contentType: "application/json",
        }).then(async ({
          data
        }) => {
          this.istrues = 1;
          if (data.isSuccess == true) {
            this.dataForm.name = data.Name;
            if (data.Sex == "男") {
              this.dataForm.sex = 1
            } else {
              this.dataForm.sex = 2
            }
            this.dataForm.certificatesNumber = data.IDCardNumber
            this.dataForm.address = data.Address;
            var list = await this.documenttypelist.filter(item => item.label == '身份证' || item.label == '居民身份证')
            if (list.length > 0) {
              this.dataForm.type = list[0].id
            }
          } else {
            this.$message({
              message: data.message,
              type: "error",
              duration: 1500
            });
          }
        })
      },
      isputile(istrues, that) {
        setTimeout(function() {
          if (istrues == 0) {
            that.$message({
              message: "接口程序未打开，请打开接口程序",
              type: "error",
              duration: 1500
            });
          }
        }, 2000);

      },
      init(id) {
        this.zjlx();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinpeople/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tHotelFloorRoomCheckinPeople.sellerId
                this.dataForm.creatTime = data.tHotelFloorRoomCheckinPeople.creatTime
                this.dataForm.type = data.tHotelFloorRoomCheckinPeople.type
                this.dataForm.name = data.tHotelFloorRoomCheckinPeople.name
                this.dataForm.tel = data.tHotelFloorRoomCheckinPeople.tel
                this.dataForm.certificatesNumber = data.tHotelFloorRoomCheckinPeople.certificatesNumber
                this.dataForm.floorRoomId = data.tHotelFloorRoomCheckinPeople.floorRoomId
                this.dataForm.address = data.tHotelFloorRoomCheckinPeople.address
                this.dataForm.arrivalsTime = data.tHotelFloorRoomCheckinPeople.arrivalsTime
                this.dataForm.predissociationTime = data.tHotelFloorRoomCheckinPeople.predissociationTime
                this.dataForm.payment = data.tHotelFloorRoomCheckinPeople.payment
                this.dataForm.roomPrice = data.tHotelFloorRoomCheckinPeople.roomPrice
                this.dataForm.consumption = data.tHotelFloorRoomCheckinPeople.consumption
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(
                `/hotel/thotelfloorroomcheckinpeople/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'creatTime': this.dataForm.creatTime,
                'type': this.dataForm.type,
                'name': this.dataForm.name,
                'tel': this.dataForm.tel,
                'certificatesNumber': this.dataForm.certificatesNumber,
                'floorRoomId': this.dataForm.floorRoomId,
                'address': this.dataForm.address,
                'arrivalsTime': this.dataForm.arrivalsTime,
                'predissociationTime': this.dataForm.predissociationTime,
                'payment': this.dataForm.payment,
                'roomPrice': this.dataForm.roomPrice,
                'consumption': this.dataForm.consumption
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {

                    this.visible = false;
                    this.$emit('refreClosing');
                    this.$emit('refreshDataList')
                  }
                });
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
